﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;

namespace TS_XY_Train_Repaire.UserCon.Stuff
{
    public partial class uc_Stuff_Main_Info_ManaForm : DevExpress.XtraEditors.XtraForm
    {
        public string _RSID, _RBID, _DepID, _PosID, _LoadID, _LoadName, _LoadType, _LoadTag,_LoadDepName = "";
        public static string _rDepID, _rDepName = "";
        public uc_Stuff_Main_Info_ManaForm()
        {
            InitializeComponent();
        }
        private void uc_Stuff_Main_Info_ManaForm_Load(object sender, EventArgs e)
        {
            TS_Win_Class.FillListTypeAct(cb_GB, "select f_id,f_name from TS_S_T_P_CadreRank where f_del=0");
            TS_Win_Class.FillListTypeAct(cb_JS, "select f_id,f_name from TS_S_T_P_ProfessionSkill where f_del=0");
            TS_Win_Class.FillListTypeAct(cb_GW, "select f_id,f_name from TS_S_T_P_StationBiaoShi where f_del=0");
            TS_Win_Class.FillListTypeAct(cb_SX, "select f_id,f_name from TS_S_T_P_PersonAttribute where f_del=0");
            TS_Win_Class.FillListTypeAct(cb_GZ, "select f_id,f_name from TS_S_T_P_PersonBranch where f_del=0");
            if (_LoadTag == "Blank")
            {
                bt_Dep.Tag = _DepID;
                tb_Dep.Text = _LoadDepName;
                TS_Win_Class.FillListTypeAct(cb_Pos, "select f_id,f_name from TS_S_T_P_Position where f_id in (select f_posid from TS_S_T_P_Dep_Position where f_depid=" + bt_Dep.Tag.ToString() + " and f_del=0) and f_del=0");
                try
                {
                    cb_Pos.SelectedValue = _PosID;
                }
                catch { }
                bcc_Code.Text = Guid.NewGuid().ToString();
            }
            else
            {
                string strSQL = "select *,(select f_name from TS_S_T_P_Department where f_id=TS_S_T_P_Stuff.f_depid) as f_depname from TS_S_T_P_Stuff where f_id=" + _LoadID;
                SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
                conn.Open();
                SqlCommand cmd = new SqlCommand(strSQL, conn);
                SqlDataReader read = cmd.ExecuteReader();
                while (read.Read())
                {
                    bcc_Code.Text = read["f_guid"].ToString();
                    tb_Name.Text = read["f_name"].ToString();
                    tb_WorkerCode.Text = read["f_workercode"].ToString();
                    cb_Sex.Text = read["f_sex"].ToString();
                    tb_Dep.Text = read["f_depname"].ToString();
                    bt_Dep.Tag = read["f_depid"].ToString();
                    TS_Win_Class.FillListTypeAct(cb_Pos, "select f_id,f_name from TS_S_T_P_Position where f_id in (select f_posid from TS_S_T_P_Dep_Position where f_depid=" + bt_Dep.Tag.ToString() + " and f_del=0) and f_del=0");
                    try
                    {
                        cb_Pos.SelectedValue = read["f_posid"].ToString();
                    }
                    catch { }
                    cb_GB.SelectedValue = read["f_gbid"].ToString();
                    cb_JS.SelectedValue = read["f_zyjsid"].ToString();
                    cb_GW.SelectedValue = read["f_gwid"].ToString();
                    cb_SX.SelectedValue = read["f_rysxid"].ToString();
                    cb_GZ.SelectedValue = read["f_gzid"].ToString();
                    rb_0.Checked = false;
                    rb_3.Checked = false;
                    rb_4.Checked = false;
                    switch (read["f_datarank"].ToString())
                    {
                        case "0":
                            rb_0.Checked = true;
                            break;
                        case "3":
                            rb_3.Checked = true;
                            break;
                        case "4":
                            rb_4.Checked = true;
                            break;
                    }
                    tb_JP.Text = TS_Win_Class.GetChineseSpell(tb_Name.Text);
                    tb_MZ.Text = read["f_mz"].ToString();
                    cb_ZZMM.Text = read["f_zzmm"].ToString();
                    tb_WHCD.Text = read["f_whcd"].ToString();
                    tb_ZY.Text = read["f_zy"].ToString();
                    cb_HYZK.Text = read["f_hyzk"].ToString();
                    tb_Phone.Text = read["f_jtdh"].ToString();
                    tb_BYYX.Text = read["f_byyx"].ToString();
                    tb_Birthday.Text = read["f_csrq"].ToString();
                    tb_GWDay.Text = read["f_gwqssj"].ToString();
                }
                read.Close();
                conn.Close();
                conn.Dispose();
            }
            switch (_LoadType)
            {
                case "Add":
                    bt_Sure.Text = "新增人员";
                    break;
                case "Edit":
                    bt_Sure.Text = "修改人员";
                    BindStuffPhoto();
                    break;
                case "Del":
                    bt_Sure.Text = "删除人员";
                    BindStuffPhoto();
                    break;
            }
        }
        private void tb_Name_TextChanged(object sender, EventArgs e)
        {
            if (tb_Name.Text.Trim().Length > 0)
            {
                tb_JP.Text = TS_Win_Class.GetChineseSpell(tb_Name.Text.Trim());
            }
            else
            {
                tb_JP.Text = "";
            }
            _ButtonEnable();
        }
        private void _ButtonEnable()
        {
            try
            {
                if (tb_Name.Text.Trim().Length > 0 && tb_WorkerCode.Text.Trim().Length > 0 && cb_Sex.Text.Trim().Length > 0 && bt_Dep.Tag.ToString().Trim().Length > 0 && cb_Pos.SelectedIndex > -1 && cb_GB.SelectedIndex > -1 && cb_GW.SelectedIndex > -1 && cb_JS.SelectedIndex > -1 && cb_SX.SelectedIndex > -1)
                {
                    bt_Sure.Enabled = true;
                }
                else
                {
                    bt_Sure.Enabled = false;
                }
            }
            catch
            {
                bt_Sure.Enabled = false;
            }
        }
        private void bt_Exit_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void bt_Sure_Click(object sender, EventArgs e)
        {
            string f_datarank = "9";
            if (rb_0.Checked)
            {
                f_datarank = "0";
            }
            if (rb_3.Checked)
            {
                f_datarank = "3";
            }
            if (rb_4.Checked)
            {
                f_datarank = "4";
            }
            string strSQL = "";
            switch (_LoadType)
            {
                case "Add":
                     strSQL = "insert into TS_S_T_P_Stuff(f_name,f_workercode,f_rbid,f_rsid,f_depid,f_posid,f_gbid,f_zyjsid,f_gwid,f_rysxid,f_datarank,f_phone,f_jp,f_sex,f_csrq,f_mz,f_whcd,f_zzmm,f_gwqssj,f_zy,f_hyzk,f_byyx,f_jtdh,f_workerid) values('"
                        +tb_Name.Text.Trim()+"','"
                        +tb_WorkerCode.Text.Trim()+"',"
                        +_RBID+","
                        +_RSID+","
                        +bt_Dep.Tag.ToString()+","
                        +cb_Pos.SelectedValue.ToString()+","
                        +cb_GB.SelectedValue.ToString()+","
                        +cb_JS.SelectedValue.ToString()+","
                        +cb_GW.SelectedValue.ToString()+","
                        + cb_SX.SelectedValue.ToString() + "," 
                        + f_datarank + ",'"
                        +tb_Phone.Text.Trim()+"','"
                        +tb_JP.Text.Trim()+"','"
                        +cb_Sex.Text.Trim()+"','"
                        +tb_Birthday.Text.Trim()+"','"
                        +tb_MZ.Text.Trim()+"','"
                        +tb_WHCD.Text.Trim()+"','"
                        + cb_ZZMM.Text.Trim() +"','"
                        + tb_GWDay.Text.Trim()+ "','"
                        +tb_ZY.Text.Trim()+"','"
                        +cb_HYZK.Text.Trim()+"','"
                        +tb_BYYX.Text.Trim()+"','"
                        +tb_Phone.Text.Trim()+"',"
                        +TS_Win_Locomotive_MainForm.pUserInfoMain.GuId+")";
                    break;
                case "Edit":
                     strSQL = "update TS_S_T_P_Stuff set f_name='"
                        + tb_Name.Text.Trim() + "',f_workercode='"
                        + tb_WorkerCode.Text.Trim() + "',f_depid="
                        + bt_Dep.Tag.ToString() + ",f_posid="
                        + cb_Pos.SelectedValue.ToString() + ",f_gbid="
                        + cb_GB.SelectedValue.ToString() + ",f_zyjsid="
                        + cb_JS.SelectedValue.ToString() + ",f_gwid="
                        + cb_GW.SelectedValue.ToString() + ",f_rysxid="
                        + cb_SX.SelectedValue.ToString() + ",f_datarank="
                        + f_datarank + ",f_phone='"
                        + tb_Phone.Text.Trim() + "',f_jp='"
                        + tb_JP.Text.Trim() + "',f_sex='"
                        + cb_Sex.Text.Trim() + "',f_csrq='"
                        + tb_Birthday.Text.Trim() + "',f_mz='"
                        + tb_MZ.Text.Trim() + "',f_whcd='"
                        + tb_WHCD.Text.Trim() + "',f_zzmm='"
                        + cb_ZZMM.Text.Trim() + "',f_gwqssj='"
                        + tb_GWDay.Text.Trim() + "',f_zy='"
                        + tb_ZY.Text.Trim() + "',f_hyzk='"
                        + cb_HYZK.Text.Trim() + "',f_byyx='"
                        + tb_BYYX.Text.Trim() + "',f_jtdh='"
                        + tb_Phone.Text.Trim() + "',f_workerid="
                        +TS_Win_Locomotive_MainForm.pUserInfoMain.GuId+",f_time=getdate() where f_id="+_LoadID;
                    break;
                case "Del":
                     strSQL = "update TS_S_T_P_Stuff set f_del=1,f_workerid="
                        +TS_Win_Locomotive_MainForm.pUserInfoMain.GuId+",f_time=getdate() where f_id="+_LoadID;
                    break;
            }
            if (_LoadType == "Add")
            {
                _LoadID = TS_Win_Class.MySqlComGetID(strSQL);
            }
            else
            {
                TS_Win_Class.MySqlCom(strSQL);
            }
            try
            {
                if (openFileDialog_stuffImg.FileName != "")
                {
                    string sql = "";

                    FileStream fs = new FileStream(openFileDialog_stuffImg.FileName, FileMode.Open, FileAccess.Read);
                    System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
                    byte[] bt = br.ReadBytes(Convert.ToInt32(fs.Length));
                    string base64String = Convert.ToBase64String(bt);
                    br.Close();
                    fs.Close();
                    if (TS_Win_Class.GetFilledDataTable("select * from TS_S_T_P_Stuff_Photo where f_del=0 and f_stuffid=" + _LoadID).Rows.Count == 0)
                    {
                        sql = "insert into TS_S_T_P_Stuff_Photo (f_stuffid,f_Photo) values(" + _LoadID + ",'" + base64String + "')";
                    }
                    else
                    {
                        sql = "update TS_S_T_P_Stuff_Photo set f_Photo='" + base64String + "' where f_del=0 and f_stuffid=" + _LoadID;
                    }
                    TS_Win_Class.MySqlCom(sql);
                }
            }
            catch
            {

            }

            this.DialogResult = DialogResult.OK;
        }

        private void bt_Dep_Click(object sender, EventArgs e)
        {
            Tools.Stuff.uc_Department_Select_By_One _uc = new Tools.Stuff.uc_Department_Select_By_One();
            _uc._Depid = bt_Dep.Tag.ToString();
            if (_uc.ShowDialog() == DialogResult.OK)
            {
                bt_Dep.Tag = _rDepID;
                tb_Dep.Text = _rDepName;
                TS_Win_Class.FillListTypeAct(cb_Pos, "select f_id,f_name from TS_S_T_P_Position where f_id in (select f_posid from TS_S_T_P_Dep_Position where f_depid=" + bt_Dep.Tag.ToString() + " and f_del=0) and f_del=0");
                try
                {
                    cb_Pos.SelectedValue = _PosID;
                }
                catch { }
                _ButtonEnable();
            }
        }

        private void bt_Select_img_Click(object sender, EventArgs e)
        {
            openFileDialog_stuffImg.Filter = "图片文件|*.jpg|图片文件|*.png";
            if (openFileDialog_stuffImg.ShowDialog() == DialogResult.OK)
            {
                if (openFileDialog_stuffImg.FileName != "")
                {
                    FileInfo _fi = new FileInfo(openFileDialog_stuffImg.FileName);
                    if (_fi.Length / 1024 > 220)
                    {
                        openFileDialog_stuffImg.FileName = "";
                        MessageBox.Show("照片应小于220KB,请重新选择！");
                    }
                    else
                    {
                        pb_Photo.ImageLocation = openFileDialog_stuffImg.FileName;
                    }
                }
            }
        }
        private void BindStuffPhoto()
        {
            try
            {
                pb_Photo.Image = null;
                DataTable dt = TS_Win_Class.GetFilledDataTable("select f_photo from TS_S_T_P_Stuff_Photo where f_del=0 and f_stuffid=" + _LoadID);
                if (dt.Rows.Count > 0)
                {
                    MemoryStream ms = new MemoryStream(Convert.FromBase64String(dt.Rows[0]["f_photo"].ToString()));
                    Image image = Image.FromStream(ms, true);
                    pb_Photo.Image = image;
                }
            }
            catch
            {

            }

        }
    }
}